<template>
  <el-dialog
    :visible.sync="dialog"
    :title="'确认提交选择的 ' + itemSize + ' 条item信息?'"
    :close-on-click-modal="false"
    width="550px"
    append-to-body
    @close="cancel"
  >
    <div>
      <el-form ref="bpmsForm" :model="bpmsParam" :rules="rules" size="small" label-width="180px">
        <el-form-item label="采购ID：" prop="auditBuyerId">
          <span>{{ bpmsParam.auditBuyerId }}</span>
        </el-form-item>
        <el-form-item label="陈列专员ID：" prop="moularId">
          <el-input v-model="bpmsParam.moularId" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="补货经理ID：" prop="replId">
          <el-input v-model="bpmsParam.replId" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="直接进口采购ID：" prop="dibuyerId">
          <el-input v-model="bpmsParam.dibuyerId" placeholder="仅供邮件通知" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="自有品牌采购ID：" prop="cateBuyerId">
          <el-input v-model="bpmsParam.cateBuyerId" placeholder="仅供邮件通知" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="bpmsParam.remark" rows="5" type="textarea" size="mini" style="width: 280px;" />
        </el-form-item>
        <el-form-item label="是否加急：">
          <el-checkbox v-model="urgentChecked" size="mini" />
          <span style="color:red;">注意：加急审批会提高审批级别！</span>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" type="text" @click="cancel">取消</el-button>
      <el-button size="mini" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUser } from '@/api/api'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    var validateUser = (rule, value, callback) => {
      if (value) {
        getUser(value).then(user => {
          if (!user) {
            callback(new Error('无效的USER ID'))
          } else {
            callback()
          }
        })
      } else {
        callback()
      }
    }
    return {
      dialog: false,
      itemSize: 0,
      urgentChecked: false,
      bpmsParam: {},
      rules: {
        moularId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        replId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        dibuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        cateBuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        auditBuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.dialog = false
      this.urgentChecked = false
    },
    doSubmit() {
      this.$refs.bpmsForm.validate((valid) => {
        if (valid) {
          this.bpmsParam.urgentFlag = this.urgentChecked ? 'Y' : 'N'
          this.parent.submitBPMS(this.bpmsParam)
          this.dialog = false
        }
      })
    }
  }
}
</script>

